/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JCategoryTreeDialog.java
 *
 * Created on 2010-1-7, 13:49:28
 */

package com.openbravo.pos.panels;

import com.openbravo.pos.base.AppLocal;
import e.odbo.data.bean.I_Category;
import cn.ekuma.epos.swing.tree.JTreePanel;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;


/**
 *
 * @author Administrator
 */
public class JCategoryTreeDialog extends javax.swing.JDialog {
    List<I_Category> datas;
    JTreePanel treePanel;
    I_Category inCat,selected;

    List<I_Category> hotList,curList;
    boolean isOK;

    /** Creates new form JCategoryTreeDialog */
    public JCategoryTreeDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
    }

    private JCategoryTreeDialog(Dialog dialog, boolean b) {
        super(dialog,b);
    }

    public static JCategoryTreeDialog newJDialog(Component ticketsbagshared) {
        Window window = getWindow(ticketsbagshared);
        JCategoryTreeDialog mydialog;
        if (window instanceof Frame) {
            mydialog = new JCategoryTreeDialog((Frame) window, true);
        } else {
            mydialog = new JCategoryTreeDialog((Dialog) window, true);
        }
        return mydialog;
    }

    private static Window getWindow(Component parent) {
        if (parent == null) {
            return new JFrame();
        } else if (parent instanceof Frame || parent instanceof Dialog) {
            return (Window) parent;
        } else {
            return getWindow(parent.getParent());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jHotToggle = new javax.swing.JToggleButton();
        jPanel7 = new javax.swing.JPanel();
        m_jButtonOK = new javax.swing.JButton();
        m_jButtonCancel = new javax.swing.JButton();
        jSelectCatLabel = new javax.swing.JLabel();

        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        jButton2.setText(AppLocal.getIntString("label.prodcategory"));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("pos_messages"); // NOI18N
        jButton1.setText(bundle.getString("label.all")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);

        jHotToggle.setText(bundle.getString("label.categoryPanel.ishot")); // NOI18N
        jHotToggle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jHotToggleActionPerformed(evt);
            }
        });
        jPanel1.add(jHotToggle);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        m_jButtonOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_ok.png"))); // NOI18N
        m_jButtonOK.setText(AppLocal.getIntString("Button.OK")); // NOI18N
        m_jButtonOK.setFocusPainted(false);
        m_jButtonOK.setFocusable(false);
        m_jButtonOK.setMargin(new java.awt.Insets(8, 16, 8, 16));
        m_jButtonOK.setRequestFocusEnabled(false);
        m_jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_jButtonOKActionPerformed(evt);
            }
        });

        m_jButtonCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_cancel.png"))); // NOI18N
        m_jButtonCancel.setText(AppLocal.getIntString("Button.Cancel")); // NOI18N
        m_jButtonCancel.setFocusPainted(false);
        m_jButtonCancel.setFocusable(false);
        m_jButtonCancel.setMargin(new java.awt.Insets(8, 16, 8, 16));
        m_jButtonCancel.setRequestFocusEnabled(false);
        m_jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_jButtonCancelActionPerformed(evt);
            }
        });

        jSelectCatLabel.setText("jLabel1");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSelectCatLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(m_jButtonOK)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(m_jButtonCancel)
                .addGap(25, 25, 25))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(m_jButtonOK, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jSelectCatLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(m_jButtonCancel, 0, 0, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel7, java.awt.BorderLayout.PAGE_END);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-618)/2, (screenSize.height-475)/2, 618, 475);
    }// </editor-fold>//GEN-END:initComponents

    private void m_jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jButtonOKActionPerformed
        if((this.inCat==null&&this.selected!=null)||(this.inCat!=null&&!inCat.equals(selected)))
            this.isOK=true;
        dispose();
}//GEN-LAST:event_m_jButtonOKActionPerformed

    private void m_jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jButtonCancelActionPerformed
        dispose();
}//GEN-LAST:event_m_jButtonCancelActionPerformed

    private void jHotToggleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jHotToggleActionPerformed
        resetList();
    }//GEN-LAST:event_jHotToggleActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       curList=this.datas;
       buildHotList();
       resetList();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        buildParentOnly();
        resetList();        
    }//GEN-LAST:event_jButton2ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JToggleButton jHotToggle;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JLabel jSelectCatLabel;
    private javax.swing.JButton m_jButtonCancel;
    private javax.swing.JButton m_jButtonOK;
    // End of variables declaration//GEN-END:variables

    public  void showDialog(List<I_Category> datas,I_Category incat) {
          initComponents();
          isOK=false;
          if(datas==null)
              datas=new ArrayList();
          this.curList=this.datas=datas;
          this.inCat=incat;
          buildHotList();
          treePanel=new JTreePanel();
          getContentPane().add(treePanel, BorderLayout.CENTER);
          treePanel.setToolbar(jPanel1);
          treePanel.addListSelectionListener(new ListSelectionListener(){
            @Override
            public void valueChanged(ListSelectionEvent e) {
                 selected=(I_Category) ((JList)e.getSource()).getSelectedValue();
                 jSelectCatLabel.setText(selected==null?"":selected.toString());
            }
          });
          treePanel.addTreeSelectionListener(new TreeSelectionListener(){
            @Override
            public void valueChanged(TreeSelectionEvent e) {
               I_Category selectCat=(I_Category) ((DefaultMutableTreeNode)e.getPath().getLastPathComponent()).getUserObject();
               buildShowList(selectCat);
               resetList();
            }
          });
          treePanel.addListMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount()==2)
                     m_jButtonOKActionPerformed(null);
            }

          });
         treePanel.setTreeRootNode(buildTree(null));
         jHotToggle.setSelected(true);
         jSelectCatLabel.setText(incat==null?"":incat.toString());
         resetList();
         this.setVisible(true);
    }

    private void resetList(){
        if(!jHotToggle.isSelected())
          treePanel.setListDates(curList);
        else
          treePanel.setListDates(hotList);
    }

    private MutableTreeNode buildTree(DefaultMutableTreeNode parent){
        if(parent==null)
             parent=new DefaultMutableTreeNode();
        Object superObj=parent.getUserObject();
        List tempList=new ArrayList();
        for(I_Category cat:datas){
            if(cat!=null&&superObj==null&&cat.getM_sParentID()==null){
                parent.add(new DefaultMutableTreeNode(cat));
            }/** else if(superObj!=null){//用于后断
               ProductCategory superCategory=(ProductCategory)superObj;
               if(superCategory.getID().equals(cat.getM_sParentID()))
                   parent.add(new DefaultMutableTreeNode(cat));
            }**/

        }
        return parent;
    }

    private void buildParentOnly(){
        curList=new ArrayList<I_Category>();
        for(I_Category cat:datas){
            if(cat!=null&&cat.getM_sParentID()==null)
                   curList.add(cat);
        }
        buildHotList();
    }

    private void buildHotList(){
       hotList=new ArrayList<I_Category>();
        for(I_Category cat:curList){
              if(cat!=null&&cat.isHot())
                  hotList.add(cat);
        }
    }

    private void buildShowList(I_Category superCategory){
        curList=new ArrayList<I_Category>();
        for(I_Category cat:datas){
            if(cat!=null&&superCategory.getID().equals(cat.getM_sParentID()))
                   curList.add(cat);
        }
        buildHotList();
    }

    public boolean isOK() {
        return isOK;
    }

    public I_Category getSelected() {
        return selected;
    }
}
